var util = require('util');

var LogManager = function(options) {
	if (typeof options != 'object')
		options = {};

	this.log = [];
	this.level = options.level || 1;
	this.display = options.display || 1;
};

LogManager.prototype.message = function(m) {
	if (this.level >= 2) {
		var message = {
			type : 'message',
			message : m,
			date : Date.now()
		};
//		this.log.push(message);

		if (this.display >= 2)
			util.puts("Message (" + message.date + "): " + message.message);
	}
};

LogManager.prototype.debug = function(d) {
	if (this.level >= 3) {
		var message = {
			type : 'debug',
			message : d,
			date : Date.now()
		};
//		this.log.push(message);

		if (this.display >= 3)
			util.puts("Debug (" + message.date + "): " + message.message);
	}
};

LogManager.prototype.error = function(e, _info) {
	if (this.level >= 1) {
		var err = {
			type : 'error',
			message : e.message,
			info : _info || '',
			stack : e.stack,
			date : Date.now()
		};
		if(this.log.length > 100)
			this.log.splice(0, 1); // Prune log entries.
//		this.log.push(err);

		if (this.display >= 1)
			util.puts("Error (" + err.date + ") " + err.info + ": " + err.message
					+ "\r\n------------------------------------\r\n"
					+ err.stack);
	}
};

module.exports = LogManager;
